var express = require('express')
var router = express.Router()

var dataModel = require('../models/data-model')

router.get('/', function(req, res ,next) {
    res.render('admin', {pagetitle: '管理'})
})

router.get('/announce', function(req, res, next) {
    dataModel.readTable('announce', function(data) {
        res.render('admin-announce', {
            pagetitle: '编辑announcement',
            announcements: data
        })
    })
})

router.get('/update/announce', function(req, res, next) {
    let cb = function() {
        res.redirect('/admin/announce')
    }
    if(req.body.action == 'delete') {
        dataModel.deleteRow('announce', req.body.id, cb)
    } else {
        dataModel.updateRow('announce', req.body, cb)
    }
})

router.post('/create/announce', function(req, res, next) {
    dataModel.createRow('announce', function() {
        res.redirect('/admin/announce#footer')
    })
})

router.get('/items', function(req,res,next) {
    dataModel.readTable('items', function(data) {
        Object.keys(data).forEach(function(id) {
            data[id].price = (parseFloat(data[id].price)/100).toFixed(2)
        })
        res.render('admin-items', {
            pagetitle: '编辑items',
            items: data
        })
    })
})

router.post('/update/item', function(req, res, next) {
    let cb = function() {
        res.redirect('/admin/items')
    }
    if(req.body.action === 'delete') {
        dataModel.deleteRow('items', req.body.id, cb)
    } else {
        req.body.price = Math.floor(parseFloat(req.body.price) *100)
        dataModel.updateRow('items', req.body, cb)
    }
})

router.post('/create/item', function(req, res, next) {
    dataModel.createRow('items', function() {
        res.redirect('/admin/items#footer')
    })
})

router.get('/events', function(req, res, next) {
    dataModel.readTable('events', function(data) {
        res.render('admin-events', {
            pagetitle: '编辑event',
            events: data
        })
    })
})

router.post('/update/event', function(req, res, next) {
    let cb = function() {
        res.redirect('/admin/events')
    }
    if(req.body.action === 'delete') {
        dataModel.deleteRow('events', req.body.id, cb)
    } else {
        dataModel.updateRow('events', req.body, cb)
    }
})

router.post('/create/event', function(req, res, next) {
    dataModel.createRow('events', function() {
        res.redirect('/admin/events#footer')
    })
})

router.get('/motd', function(req, res, next) {
    dataModel.readTable('motd', function(data) {
        res.render('admin-motd', {
            pagetitle: '编辑每日寄语',
            motd: data
        })
    })
})

router.post('/update/motd' ,function(req, res, next) {
    let cb = function() {
        res.redirect('/admin/motd')
    }
    if(req.body.action === 'delete') {
        dataModel.deleteRow('motd', req.body.id, cb)
    } else {
        dataModel.updateRow('motd', req.body, cb)
    }
})

router.post('/create/motd', function(req, res, next) {
    dataModel.createRow('motd', function() {
        res.redirect('/admin/motd')
    })
})

module.exports = router
